//
//  SendWeiBoView.swift
//  NewWeiBo
//
//  Created by 吴天益 on 2017/7/30.
//  Copyright © 2017年 ttt. All rights reserved.
//

import UIKit
import pop

class SendWeiBoView: UIView {
    
    var target: UIViewController?
    
    //pilst文件数组
    private lazy var modelArray: [SendButtonModel] = {
    
        let path = Bundle.main.path(forResource: "sendbutton.plist", ofType: nil)
        
        let arr = NSArray(contentsOfFile: path!)!
        
        let arr2 = NSArray.yy_modelArray(with: SendButtonModel.self, json: arr) as! [SendButtonModel]
        
        return arr2
    }()
    
    //懒加载一个按钮数组
    private lazy var btnArray: [TTCostomButton] = [TTCostomButton]()
    
    //懒加载模糊图片
    private lazy var bgImageView: UIImageView = {
        //截屏,并为截屏图片添加模糊效果,模糊效果第三方是OC语言,要调用需要桥接文件
        let image: UIImage = getScreenShot().applyLightEffect()
        
//        var imageView = UIImageView()
//        imageView.image = image
        let imageView = UIImageView(image: image)
        
        return imageView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 界面设置
    func setupUI() {
        
        addSubview(bgImageView)
        
        //添加6个按钮
        addButton()
        
        //6个按钮
        theAnimationsOfButton()
 
    }
    
    
    //MARK: - 发微博视图出现后的按钮的动画效果
    func theAnimationsOfButton() {
        //使用pop框架实现动画效果
        for (i,button) in btnArray.enumerated() {
            
            let animation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            
            var index = i
            //-350 就是移动的距离
            if(button.center.y > ScreenHeight){
                animation?.toValue = CGPoint(x: button.center.x, y: button.center.y - 350)
            }else{
                animation?.toValue = CGPoint(x: button.center.x, y: button.center.y + 350)
                
                index = btnArray.count - 1 - i
            }
            
            //设置弹簧系数,0-20,值越大,震动幅度越大
            animation?.springBounciness = 10
            
            //设置加速度,默认12,值越大,就越快
            animation?.springSpeed = 10
            
            //设置开始时间  CACurrentMediaTime()应该就是animation创建的时间
            animation?.beginTime = CACurrentMediaTime() + Double(index) * 0.1
            
            //把动画添加到按钮上
            button.pop_add(animation, forKey: nil)
        }
  
    }
    
    
    
    //MARK: - 添加6个按钮
    func addButton() {
        
        for i in 0..<6 {
            
            let button = TTCostomButton()
            
//            button.imageView?.image = UIImage(named: "logo")
//            button.titleLabel?.text = "瞧一瞧看一看"
            button.setImage(UIImage(named: modelArray[i].imageName!), for: .normal)
            button.setTitle(modelArray[i].title, for: .normal)
            button.setTitleColor(UIColor.black, for: .normal)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            
            button.tag = i
            
            let btnWidth = 60
            let btnHeight = 80
            //列数
            let colNum = 3
            //列索引
            let colIndex = i % 3
            //行索引
            let rowIndex = i / 3
            //间距
            let margin = (Int(ScreenWidth) - colNum * btnWidth) / (colNum + 1)
            
            let btnX = colIndex * btnWidth + (colIndex + 1) * margin
            //加一个屏幕高度的y,使其一开始在屏幕下方,方便后面的动画
            let btnY = rowIndex * btnHeight + (rowIndex + 1) * margin + Int(ScreenHeight)
            
            button.frame = CGRect(x: btnX, y: btnY, width: btnWidth, height: btnHeight)
            
            //按钮监听事件
            button.addTarget(self, action: #selector(clickButton(sender:)), for: .touchUpInside)
            
            addSubview(button)
            
            btnArray.append(button)
        }
   
    }
    
    //MARK: - 按钮监听事件方法实现,点击按钮变大
    func clickButton(sender: UIButton) {
        
        for button in btnArray{
            
            UIView.animate(withDuration: 0.4, animations: {
                
                if button == sender {
                    button.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
                }else{
                    button.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
                }
            }, completion: { (_) in
                
                UIView.animate(withDuration: 0.2, animations: {
                    button.transform = CGAffineTransform.identity
                }, completion: { (_) in
                    
                })
            })
        }
        
        //延迟0.4 + 0.2秒后
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
            
            //字符串转类
            let vcName = self.modelArray[sender.tag].controllerName
            
            let cls = NSClassFromString("NewWeiBo.\(vcName!)") as! UIViewController.Type
            
            let controller = cls.init()
            
            //新建导航栏
            let navi = TTNavigationController(rootViewController: controller)
            
            self.target?.present(navi, animated: true, completion: {
                self.removeFromSuperview()
            })
            
        }
        
    }
    
    //MARK: - 点击屏幕触发方法
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
//        if let e = event {
//            print(e)
//        }
        
        theAnimationsOfButton()
        
        //延迟0.1 * 6 后销毁
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) { 
            self.removeFromSuperview()
        }
    }


}

//MARK:
//MARK: - 自定义按钮
class TTCostomButton: UIButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        imageView?.contentMode = .center
        
        titleLabel?.textAlignment = .center
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 调整默认按钮子视图布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = self.bounds.size.width
        let height = self.bounds.size.height
        
        //imageView
        //赋值最好用bounds,frame会变
        imageView?.frame = CGRect(x: 0, y: 0, width: width, height: width)
        
        //lable
        titleLabel?.frame = CGRect(x: 0, y: width, width: width, height: height-width)
    }
}
